package com.ebupt.migu.music.controller;

import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ebupt.migu.music.service.SyncTsgService;
import com.ebupt.migu.restapi.DownloadByFtp;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author liuyangyang@e-byte.com
 * @description: TODO
 * @date 2024/11/5 19:03
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    SyncTsgService service;
    @Autowired
    RedisTemplate redisTemplate;
    @Value("${division.path}")
    private String DIVISION_PATH;

    @RequestMapping("/tsg/customer")
    public void getTsgCustomer(@RequestParam("customerId") String customerId) {
        log.info("尝试从TSG获取客群 === 人群id：{}", customerId);
        Map<String, Object> strategy = new HashMap<>();
        strategy.put("strategyId", System.currentTimeMillis());
        strategy.put("strategyName", System.currentTimeMillis());
        strategy.put("groupId", customerId);
        strategy.put("approverName", System.currentTimeMillis());
        strategy.put("creatorName", System.currentTimeMillis());
        DownloadByFtp.getLabelUserGroup(strategy,
            "https://10.194.4.54:443/userlabel/music/api/customer/activity-ref/add", 1);
    }

    @RequestMapping("/export/customer")
    public void syncUserGroup(@RequestBody String str) {
        String start = String.valueOf(JSONObject.parseObject(str).get("start"));
        String end = String.valueOf(JSONObject.parseObject(str).get("end"));
        log.info("开始时间：{}，结束时间：{}", start, end);
        service.getCustomer(DateUtil.parse(start), DateUtil.parse(end));
    }

    @RequestMapping("/division")
    public Map<String, Integer> division(@RequestParam("key") String key, @RequestParam("size") Integer size) {
        try {
            if (null == size){
                size = 999999;
            }
            Cursor<String> cursor = redisTemplate.opsForSet().scan(key, ScanOptions.NONE);
            int count = 0;
            String path = "";
            String fileName = "";
            FileWriter writer = null;
            Map<String, Integer> res = new HashMap<>();
            while (cursor.hasNext()) {
                if (null == writer) {
                    fileName = key + "-" + System.currentTimeMillis() + ".csv";
                    path = DIVISION_PATH + fileName;
                    writer = new FileWriter(path);
                    res.put(fileName, size);
                }
                if (count >= size) {
                    writer.close();
                    fileName = key + "-" + System.currentTimeMillis() + ".csv";
                    path = DIVISION_PATH + fileName;
                    writer = new FileWriter(path);
                    res.put(fileName, size);
                    count = 0;
                }
                String content = cursor.next();
                writer.write(content);
                writer.write("\n");
                count++;
            }
            writer.close();
            res.put(fileName, count);
            return res;
        } catch (Exception e) {
            return null;
        }
    }
}
